<?php

namespace IDock\Insurance\ZYIC\Response;

use IDock\Http\Response;
use IDock\Insurance\ZYIC\Response\AllZYICResponse;
use IDock\Printer\EPolicyDownloadPrinter;

class EPolicyDownloadResponse extends AllZYICResponse
{

    /**
     * @var EPolicyDownloadPrinter $printer
     */
    public $printer = EPolicyDownloadPrinter::class;


    public function printerInit(): Response
    {
        $body = $this->getBody();
        $data = $body['content'] ?? [];
        if ($this->printer) {
            $this->printer = new $this->printer;
        }
        $this->printer->doing = true;
        $data = $this->formatArray($data);
        foreach ($data as $val) {
            if (empty($val)) {
                continue;
            }
            if ($val['certiType'] == '交强险保单') {
                $this->printer->finish = true;
                $this->printer->doing = false;
                $this->printer->url = $val['downLoad'] ?? '';
            }
            if ($val['certiType'] == '交强险标志') {
                $this->printer->finish = true;
                $this->printer->doing = false;
                $this->printer->signUrl = $val['downLoad'] ?? '';
            }
            if ($val['certiType'] == '商业险保单') {
                $this->printer->finish = true;
                $this->printer->doing = false;
                $this->printer->url = $val['downLoad'] ?? '';
            }
            if ($val['certiType'] == '非车' or $val['certiType'] == '乐驾保') {
                $this->printer->finish = true;
                $this->printer->doing = false;
                $this->printer->url = $val['downLoad'] ?? '';
            }
        }
        if (empty($this->printer->url)) {
            $this->setError('下载电子保单失败');
            return $this;
        }
        return $this;
    }
}
